using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class VipDiscountsEF
	{
		public VipDiscountsEF(DbContext dbContext)
		{
			vipDiscountsServerEF = new VipDiscountsServerEF(dbContext);
            discountsServerEF = new DiscountsServerEF(dbContext);
		}
		public VipDiscountsServerEF vipDiscountsServerEF { get; set; }
        public DiscountsServerEF discountsServerEF { get; set; }
	}
	[InjectClass]
	public class VipDiscountsServer : IVipDiscountsServer
	{
		private readonly VipDiscountsEF _vipDiscountsEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public VipDiscountsServer(DbContext dbContext)
		{
			_vipDiscountsEF = new VipDiscountsEF(dbContext);
		}
        public async Task<ResponseApiDto> AddAsync(RequestVipDiscountsDto dto)
        {
            return ResponseApiDto.Create(await _vipDiscountsEF.vipDiscountsServerEF.AddAsync(dto.ToCreateEntity()));
        }
        public async Task<ResponseApiDto> DeleteAsync(RequestVipDiscountsDto dto)
        {
            return ResponseApiDto.Create(await _vipDiscountsEF.vipDiscountsServerEF.DeleteAsync(dto.ToDeleteEntity()));
        }
        public async Task<ResponseApiDto> UpdateAsync(RequestVipDiscountsDto dto)
        {
            return ResponseApiDto.Create(await _vipDiscountsEF.vipDiscountsServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
        }
        public async Task<ResponseApiDto> PaginationAsync(int page, int limit)
        {
            int total = 0;
            List<ResponseVipDiscountsDto> data = new List<ResponseVipDiscountsDto>();
            List<VipDiscounts> vipDiscountss = await _vipDiscountsEF.vipDiscountsServerEF.Pagination1(d => true, false, d => d.VdCreateTime, out total, page, limit).ToListAsync();
            foreach (var vipDiscounts in vipDiscountss)
            {
                data.Add(vipDiscounts.ToResponseDto());
            }
            data.ForEach((b) =>
            {
                b.Time = Tools.TimeToString(b.createTime);
            });
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
        /// <summary>
        /// 单个查询
        /// </summary>
        /// <param id="id"></param>
        /// <returns></returns>
        public async Task<ResponseApiDto> Get_dg_Disvount(string id)
        {
            List<ResponseVipDiscountsDto> data = new List<ResponseVipDiscountsDto>();
            List<VipDiscounts> ip = await _vipDiscountsEF.vipDiscountsServerEF.Query(d => d.VdVipId == id).ToListAsync();
            ip.ForEach((b) =>
            {
                data.Add(b.ToResponseDto());
            });
            data.ForEach((b) =>
            {
                b.DDiscount = _vipDiscountsEF.discountsServerEF.Query(d => d.DId == b.discountId).Select(d => d.DDiscount).Single();
                b.DMaxDiscountCount = _vipDiscountsEF.discountsServerEF.Query(d => d.DId == b.discountId).Select(d => d.DMaxDiscountCount).Single();
                b.Time = Tools.TimeToString(b.createTime);
            });
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { data = data });
        }
    }
}
